package com.cgy.utils.base.objects;

import org.apache.commons.lang3.BooleanUtils;
import org.apache.commons.lang3.math.NumberUtils;

/**
 * object 工具类
 * @author CHTK
 */
public class ObjectHelper {
    /**
     *
     * @param obj
     * @return
     */
    public static <Entity>String execute(final Entity obj, final NullHandler<Entity> nullHandler) {
        if(null == obj && null != nullHandler) {
            return nullHandler.handler();
        }
        return null;
    }
    /**
     * object转String
     * @param object
     * @return
     */
    public static String toString(Object object) {
        return null != object ? object.toString() : "";
    }

    /**
     * object转float
     * @param object
     * @return
     */
    public static Float toFloat(Object object) {
        return NumberUtils.toFloat(toString(object));
    }
    /**
     * object转long
     * @param object
     * @return
     */
    public static Long toLong(Object object) {
        return NumberUtils.toLong(toString(object));
    }

    /**
     * object转 byte
     * @param object
     * @return
     */
    public static Byte toByte(Object object) {
        return NumberUtils.toByte(toString(object));
    }
    /**
     * object转 int
     * @param object
     * @return
     */
    public static Integer toInt(Object object) {
        return NumberUtils.toInt(toString(object));
    }
    /**
     * object转 short
     * @param object
     * @return
     */
    public static Short toShort(Object object) {
        return NumberUtils.toShort(toString(object));
    }
    /**
     * object转 double
     * @param object
     * @return
     */
    public static Double toDouble(Object object) {
        return NumberUtils.toDouble(toString(object));
    }
    /**
     * object转 boolean
     * @param object
     * @return
     */
    public static Boolean toBoolean(Object object) {
        return BooleanUtils.toBoolean(toString(object));
    }

    /**
     * 不为空
     * @param emptyHandler
     * @param values
     * @return
     */
    public static <T>T isAnyNotBlank(final AnyEmptyHandler<T> emptyHandler, final String... values) {
        if(null != values && null != emptyHandler) {
            for (String value : values) {
                if(null == value) {
                    return null;
                }
            }
            return emptyHandler.handler(values.length, values);
        }
        return null;
    }

    /**
     *
     * @param o
     * @param s
     * @return
     */
    public static String defaultIfNull(Object o, String s) {
        return null == o ? s : o.toString();
    }

    /**
     *
     */
    public static interface AnyEmptyHandler<T> {

        /**
         * 回调
         * @param size
         * @param value
         * @return
         */
        public T handler(int size, String... value);
    }
    /**
     *
     */
    public static abstract class NullHandler<T> {
        /**
         *
         * @param
         * @return
         */
        abstract public String handler();
    }
}
